package com.controller;

import com.entity.Course;
import com.entity.CourseType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.CourseService;
import com.service.CourseTypeService;
import com.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ct")
public class CtController {

    @Autowired
    CourseTypeService courseTypeService;

    @Autowired
    CourseService courseService;

    @RequestMapping("/findByParentId.do")
    public CourseType findByParentId(Integer parent_id){

        // 记录日志
        CourseType courseType = courseTypeService.findCourseByParentId(parent_id);
        return courseType;
    }


    @RequestMapping("/findCourseType.do")
    public List<CourseType> findCourseType(){
        List<CourseType> courseTypes = courseTypeService.findCtAll();
//        System.out.println("ctAll"+courseTypes);
        return courseTypes;
    }

    //后台
    @RequestMapping("/showBackName.do")
    public AjaxResult showBackName(Integer id){
        AjaxResult ajaxResult = new AjaxResult();
        List<CourseType> courseTypes = courseTypeService.showCosTypeName();
        ajaxResult.setObj(courseTypes);
        return ajaxResult;
    }

    @RequestMapping("/findBackCourseType.do")
    public PageInfo<CourseType> findBackCourseType(Integer pageNo,Integer parent_id){
        PageInfo<CourseType> pageInfo = courseTypeService.showCosTypeByPage(pageNo, parent_id);
        return pageInfo;
    }


//    url : '/ct/addBackType.do',  "type_name":type_name, "parent_id":parent_id, "status":0
    @RequestMapping("/addBackType.do")
    public AjaxResult addBackType(String type_name,Integer parent_id,Integer status){
//        System.out.println(parent_id+"---------------------------------------------------------------------------------");
        Integer integer = courseTypeService.addCourseType(type_name, parent_id, status);
        AjaxResult ajaxResult = new AjaxResult();
        if(integer>0){
            ajaxResult.setMsg("添加成功"+integer+"条");
        }else if(integer==-1){
            ajaxResult.setMsg("添加失败，类别已经存在");
        }else{
            ajaxResult.setMsg("添加失败");
        }
        return ajaxResult;
    }

//    url : '/ct/modifyCourseTypeName.do'data : "id":id, "type_name":type_name, "parent_id":parent_id,
    @RequestMapping("/modifyCourseTypeName.do")
    public AjaxResult modifyCourseTypeName(String type_name,Integer parent_id){
//        System.out.println("type_name"+type_name+"parent_id"+parent_id+"------*******************************");
        Integer integer = courseTypeService.changeCoutype(type_name, parent_id);
        AjaxResult ajaxResult = new AjaxResult();
        if(integer>0){
            ajaxResult.setMsg("修改成功"+integer+"条");
        }else{
            ajaxResult.setMsg("修改失败");
        }
        return  ajaxResult;
    }

//    url:'/ct/toggleStatus.do',data : "id":id, "status":status
    @RequestMapping("/toggleStatus.do")
    public Integer toggleStatus(Integer id,Integer status){
        Integer integer = courseTypeService.chageCourseTypeStatus(id, status);
        return integer;
    }
}
